var e = require("../@babel/runtime/helpers/interopRequireDefault");

Object.defineProperty(exports, "__esModule", {
    value: !0
}), exports.default = void 0;

var t = e(require("../@babel/runtime/regenerator")), r = require("../@babel/runtime/helpers/createForOfIteratorHelper"), a = require("../@babel/runtime/helpers/asyncToGenerator"), i = require("../@babel/runtime/helpers/defineProperty"), s = e(require("../http/order")), o = e(require("../http/address")), n = e(require("../http/coupon")), c = e(require("../http/pay")), u = require("../utils/tool"), d = getApp(), p = (require("../utils/api.js"), 
new s.default()), m = new o.default(), l = new n.default(), h = (new c.default(), 
require("../http/indexApi.js")), f = Behavior({
    properties: {
        createOrderParams: {
            type: Object
        },
        productInfo: {
            type: Object,
            observer: function(e, t) {
                var r;
                if (e.limitTime) {
                    e.limitTime = e.limitTime ? (0, u.formatIosTime)(e.limitTime) : e.limitTime;
                    var a = new Date().getTime(), s = new Date(e.limitTime).getTime();
                    this.setData({
                        isLimitTime: a > s
                    });
                } else this.setData({
                    isLimitTime: !1
                });
                this.setData((i(r = {}, "computedPriceParams.productId", e.productId), i(r, "computedPriceParams.orderType", e.activityStatus), 
                i(r, "purchasableQuantity", e.limitNum > 0 ? e.limitNum - (e.memberLimit || 0) : e.stock), 
                r));
            }
        }
    },
    data: {
        computedPriceParams: {
            productId: "",
            orderType: 1,
            quantity: 1,
            discountType: 0,
            couponHistoryId: "",
            addressId: "",
            disableCoupon: !1
        },
        orderPriceDetails: {},
        isIntegral: !1,
        purchasableQuantity: 0,
        isWholeBox: !1,
        orderId: "",
        boxId: "",
        isLimitTime: !1
    },
    lifetimes: {
        attached: function() {
            this.computedPrice(), this.setData({
                isWholeBox: this.isWholeBox()
            });
        }
    },
    methods: {
        getCouponHistoryId: function(e) {
            var t;
            this.data.computedPriceParams.couponHistoryId || e ? (this.setData((i(t = {}, "computedPriceParams.couponHistoryId", e || ""), 
            i(t, "computedPriceParams.discountType", e ? 2 : 0), t)), this.computedPrice()) : (0, 
            u.toastToPage)("当前没有选择使用的优惠券");
        },
        getCouponList: (0, u.throttle)(a(t.default.mark(function e() {
            var a, i, s, o, n, c, p, m, h;
            return t.default.wrap(function(e) {
                for (;;) switch (e.prev = e.next) {
                  case 0:
                    if (console.log(11), !this.data.disableCoupon) {
                        e.next = 3;
                        break;
                    }
                    return e.abrupt("return");

                  case 3:
                    if (e.prev = 3, !this.data.orderPriceDetails.couponContent.deductionMoney) {
                        e.next = 7;
                        break;
                    }
                    return this.triggerEvent("showCouponList", {}, {}), e.abrupt("return");

                  case 7:
                    if (this.data.orderPriceDetails.couponCount) {
                        e.next = 10;
                        break;
                    }
                    return (0, u.toastToPage)("暂无可用优惠券"), e.abrupt("return");

                  case 10:
                    return d.showLoading("加载中"), a = {
                        productId: this.data.computedPriceParams.productId,
                        quantity: this.data.computedPriceParams.quantity
                    }, e.next = 14, l.getCouponByGoods(a);

                  case 14:
                    i = e.sent, s = i.data.canUseCoupon, o = r(s);
                    try {
                        for (o.s(); !(n = o.n()).done; ) (c = n.value).select = !1, c.collectEndTime = (0, 
                        u.formatTime)(c.collectEndTime), c.collectTime = (0, u.formatTime)(c.collectTime), 
                        c.startTime = (0, u.formatTime)(c.startTime), c.endTime = (0, u.formatTime)(c.endTime), 
                        c.collectStartTime = (0, u.formatTime)(c.collectStartTime), c.collectEndTime = (0, 
                        u.formatTime)(c.collectEndTime);
                    } catch (e) {
                        o.e(e);
                    } finally {
                        o.f();
                    }
                    p = r(i.data.noCanUseList);
                    try {
                        for (p.s(); !(m = p.n()).done; ) (h = m.value).collectEndTime = (0, u.formatTime)(h.collectEndTime), 
                        h.collectTime = (0, u.formatTime)(h.collectTime), h.startTime = (0, u.formatTime)(h.startTime), 
                        h.endTime = (0, u.formatTime)(h.endTime), h.collectStartTime = (0, u.formatTime)(h.collectStartTime), 
                        h.collectEndTime = (0, u.formatTime)(h.collectEndTime);
                    } catch (e) {
                        p.e(e);
                    } finally {
                        p.f();
                    }
                    this.data.computedPriceParams.couponHistoryId ? this.triggerEvent("taping", {}, {}) : this.triggerEvent("taping", {
                        canUseCoupon: s,
                        noCanUseList: i.data.noCanUseList
                    }, {}), e.next = 26;
                    break;

                  case 23:
                    e.prev = 23, e.t0 = e.catch(3), wx.hideLoading();

                  case 26:
                  case "end":
                    return e.stop();
                }
            }, e, this, [ [ 3, 23 ] ]);
        })), 3e3),
        startPay: (0, u.throttle)(function() {
            var e = a(t.default.mark(function e(r) {
                var s, o, n, c, d, m, l, h, f, P = this;
                return t.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.prev = 0, wx.showLoading({
                            mask: !0,
                            title: "加载中…"
                        }), s = r.currentTarget.dataset.boxid || "", this.setData({
                            boxId: s
                        }), e.next = 6, this._getDefaultAddressId();

                      case 6:
                        if (o = e.sent) {
                            e.next = 11;
                            break;
                        }
                        return wx.hideLoading(), this.triggerEvent("showSendAgreement", {}), e.abrupt("return");

                      case 11:
                        if (this.setData(i({}, "computedPriceParams.addressId", o)), 0 != this.data.orderPriceDetails.discountPrice) {
                            e.next = 29;
                            break;
                        }
                        if (!s) {
                            e.next = 22;
                            break;
                        }
                        return this.setData(i({}, "computedPriceParams.boxId", s)), e.next = 17, p.createZeroBrightOrder(this.data.computedPriceParams);

                      case 17:
                        n = e.sent, this.setData({
                            orderId: n.data.orderId
                        }), this.successNews(1), e.next = 28;
                        break;

                      case 22:
                        return e.next = 24, p.createZeroBlindOrder(this.data.computedPriceParams);

                      case 24:
                        n = e.sent, wx.hideLoading(), (0, u.toastToPage)("购买成功"), this.successNews(0);

                      case 28:
                        return e.abrupt("return");

                      case 29:
                        if (!s) {
                            e.next = 37;
                            break;
                        }
                        return this.setData(i({}, "computedPriceParams.boxId", s)), e.next = 33, p.createBrightOrder(this.data.computedPriceParams);

                      case 33:
                        c = e.sent, this.setData({
                            orderId: c.data.orderId
                        }), e.next = 42;
                        break;

                      case 37:
                        return this.setData(i({}, "computedPriceParams.addressId", o)), e.next = 40, p.createBlindOrder(this.data.computedPriceParams);

                      case 40:
                        d = e.sent, this.setData({
                            orderId: d.data.orderId
                        });

                      case 42:
                        return e.next = 44, (0, u.promising)(wx.login)();

                      case 44:
                        return m = e.sent, l = m.code, e.next = 48, p.getWxPayInfo(l, this.data.orderId);

                      case 48:
                        h = e.sent, f = h.data, s ? (0, u.callWeChatPay)(f, function() {
                            var e = a(t.default.mark(function e(r) {
                                return t.default.wrap(function(e) {
                                    for (;;) switch (e.prev = e.next) {
                                      case 0:
                                        P.successNews(1);

                                      case 1:
                                      case "end":
                                        return e.stop();
                                    }
                                }, e);
                            }));
                            return function(t) {
                                return e.apply(this, arguments);
                            };
                        }(), function() {
                            var e = a(t.default.mark(function e(r) {
                                return t.default.wrap(function(e) {
                                    for (;;) switch (e.prev = e.next) {
                                      case 0:
                                        P.cancelOrder();

                                      case 1:
                                      case "end":
                                        return e.stop();
                                    }
                                }, e);
                            }));
                            return function(t) {
                                return e.apply(this, arguments);
                            };
                        }()) : (0, u.callWeChatPay)(f, function(e) {
                            P.successNews(0);
                        }, function(e) {
                            P.cancelOrder();
                        }), e.next = 56;
                        break;

                      case 53:
                        e.prev = 53, e.t0 = e.catch(0), wx.hideLoading();

                      case 56:
                        return e.prev = 56, e.finish(56);

                      case 58:
                      case "end":
                        return e.stop();
                    }
                }, e, this, [ [ 0, 53, 56, 58 ] ]);
            }));
            return function(t) {
                return e.apply(this, arguments);
            };
        }(), 300),
        hasDefaultAddress: function() {
            var e = this;
            return a(t.default.mark(function r() {
                var a;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, e._getDefaultAddressId();

                      case 2:
                        if (a = t.sent) {
                            t.next = 8;
                            break;
                        }
                        return e.triggerEvent("showSendAgreement", {}), t.abrupt("return", !1);

                      case 8:
                        return e.setData(i({}, "computedPriceParams.addressId", a)), t.abrupt("return", !0);

                      case 10:
                      case "end":
                        return t.stop();
                    }
                }, r);
            }))();
        },
        computedPrice: function() {
            var e = this;
            return a(t.default.mark(function r() {
                var a, s, o, n;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.next = 2, p.zeroDrawActivityInfo();

                      case 2:
                        return s = t.sent, t.next = 5, p.getPreviewPrice(e.data.computedPriceParams);

                      case 5:
                        o = t.sent, n = !1, s.data.activityVo && (o.data.discountPrice = 0, n = !0), e.setData((a = {
                            orderPriceDetails: o.data,
                            disableCoupon: n
                        }, i(a, "computedPriceParams.discountType", o.data.discountType), i(a, "computedPriceParams.couponHistoryId", o.data.couponContent ? o.data.couponContent.couponHistoryId : ""), 
                        i(a, "computedPriceParams.orderType", o.data.orderType), a));

                      case 9:
                      case "end":
                        return t.stop();
                    }
                }, r);
            }))();
        },
        addQuantity: function() {
            if (!this._isLimit()) {
                var e, t = this.properties.computedPriceParams.quantity;
                this.setData((i(e = {}, "computedPriceParams.quantity", t + 1), i(e, "computedPriceParams.discountType", 0), 
                i(e, "computedPriceParams.couponHistoryId", ""), e)), this._resetDiscount(), this.computedPrice();
            }
        },
        reduceQuantity: function() {
            var e, t = this.properties.computedPriceParams.quantity;
            t > 1 ? (this.setData((i(e = {}, "computedPriceParams.quantity", t - 1), i(e, "computedPriceParams.discountType", 0), 
            i(e, "computedPriceParams.couponHistoryId", ""), e)), this._resetDiscount(), this.computedPrice()) : (0, 
            u.toastToPage)("购买数量最低为1!");
        },
        chooseIntegral: function(e) {
            var t;
            if (!this.data.orderPriceDetails.sureIntegrationNum) return (0, u.toastToPage)("暂无可用积分"), 
            void this.setData({
                isIntegral: !1
            });
            this.setData((i(t = {}, "computedPriceParams.discountType", e.detail.select ? 1 : 0), 
            i(t, "isIntegral", e.detail.select), t)), this.computedPrice();
        },
        _isLimtiBuyTime: function() {
            return this.properties.productInfo.limitNum > 0 && new Date().getTime() > new Date(this.properties.productInfo.limitTime).getTime();
        },
        _isLimit: function() {
            var e = this.properties.computedPriceParams.quantity;
            return this._isLimtiBuyTime() ? e >= this.data.purchasableQuantity && ((0, u.toastToPage)("已达到最大购买数量!"), 
            !0) : e >= this.properties.productInfo.stock && ((0, u.toastToPage)("超过库存啦！"), !0);
        },
        _getDefaultAddressId: function() {
            return a(t.default.mark(function e() {
                var r;
                return t.default.wrap(function(e) {
                    for (;;) switch (e.prev = e.next) {
                      case 0:
                        return e.next = 2, m.getDefaultAddress();

                      case 2:
                        return r = e.sent, e.abrupt("return", r.data.addressId);

                      case 4:
                      case "end":
                        return e.stop();
                    }
                }, e);
            }))();
        },
        _resetDiscount: function() {
            var e;
            this.setData((i(e = {}, "computedPriceParams.discountType", 0), i(e, "computedPriceParams.couponHistoryId", ""), 
            i(e, "isIntegral", !1), e));
        },
        isWholeBox: function() {
            if (0 == this.properties.productInfo.boxStatus) {
                var e = this.properties.productInfo.stock, t = this.properties.productInfo.boxNum;
                return !(t > e || t > this.data.purchasableQuantity);
            }
            return !1;
        },
        buyWholeBox: function() {
            var e = this.properties.productInfo.boxNum, t = this.data.computedPriceParams.quantity;
            if (this.data.computedPriceParams.quantity % e == 0) {
                var r;
                if (t + e > this.properties.productInfo.limitNum && this._isLimtiBuyTime()) return void (0, 
                u.toastToPage)("超出限购数量!");
                if (t + e > this.properties.productInfo.stock) return void (0, u.toastToPage)("超过库存啦！");
                this.setData((i(r = {}, "computedPriceParams.quantity", t + e), i(r, "computedPriceParams.discountType", 0), 
                i(r, "computedPriceParams.couponHistoryId", ""), r));
            } else {
                var a;
                this.setData((i(a = {}, "computedPriceParams.quantity", e), i(a, "computedPriceParams.discountType", 0), 
                i(a, "computedPriceParams.couponHistoryId", ""), a));
            }
            this.computedPrice();
        },
        blindBoxPaySuccess: function() {
            var e = this;
            wx.hideLoading(), (0, u.toastToPage)("支付成功"), d.showConfirm("温馨提醒", "支付成功！请到【我的-我的盒柜】操作发货，以完成下单！", function(t) {
                1 == t ? (e.triggerEvent("onClose", {}), wx.navigateTo({
                    url: "/pages/order/order?orderType=0"
                })) : e.triggerEvent("onClose", {});
            }, !0, "去盒柜", "取消");
        },
        brightBoxSucces: function() {
            var e = this;
            return a(t.default.mark(function r() {
                var a;
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.prev = 0, (0, u.toastToPage)("支付成功"), d.showLoading("开盒中…"), t.next = 5, 
                        p.getOpenResult(e.data.orderId, e.data.boxId);

                      case 5:
                        a = t.sent, wx.hideLoading(), e.triggerEvent("onOpenSuccess", a.data), t.next = 15;
                        break;

                      case 10:
                        t.prev = 10, t.t0 = t.catch(0), console.log(t.t0), wx.hideLoading(), (0, u.toastToPage)("开盒失败");

                      case 15:
                      case "end":
                        return t.stop();
                    }
                }, r, null, [ [ 0, 10 ] ]);
            }))();
        },
        cancelOrder: function() {
            var e = this;
            return a(t.default.mark(function r() {
                return t.default.wrap(function(t) {
                    for (;;) switch (t.prev = t.next) {
                      case 0:
                        return t.prev = 0, t.next = 3, p.cancelOrder(e.data.orderId);

                      case 3:
                        wx.hideLoading(), (0, u.toastToPage)("订单已取消"), e.triggerEvent("onClose", {}), t.next = 11;
                        break;

                      case 8:
                        t.prev = 8, t.t0 = t.catch(0), wx.hideLoading();

                      case 11:
                      case "end":
                        return t.stop();
                    }
                }, r, null, [ [ 0, 8 ] ]);
            }))();
        },
        successNews: function(e) {
            var t = this;
            wx.hideLoading();
            var r = this;
            wx.requestSubscribeMessage({
                tmplIds: [ "vBPr22DDXOhYMu7o3hc3k055YKUvYSmncOQjkcVzLE0", "hdkwX1PFKY08Wrw3z18S6oFpn2SeoyToVW4qGW2L-4w", "C1t5mpxs6FRapLx2Ht1gnmwpDyKwJj1HkEzWJ_mv6R0" ],
                success: function(e) {
                    h.subscribeNotice({
                        messageType: 7,
                        messageId: r.data.orderId
                    }).then(function(e) {}).catch(function(e) {});
                },
                fail: function(e) {
                    console.log(e);
                },
                complete: function(r) {
                    0 == e ? t.blindBoxPaySuccess() : t.brightBoxSucces();
                }
            });
        }
    }
});

exports.default = f;